package com.hzhc.controller.yworder;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.CntrImgFile;
import com.hzhc.entity.YwOrderCntr;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.YwOrderCntrService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.YwOrderCntrVo;
import com.hzhc.vo.YwOrderVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 箱明细
 *
 * @author Fchenxu
 * @since 2024-05-27
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/ywOrderCntr")
public class YwOrderCntrController extends BaseController {

    private final YwOrderCntrService ywOrderCntrService;

    @Value("${file.cntrUploadTempFolder}")
    private String cntrUploadTempFolder;

    @Value("${file.cntrUploadFolder}")
    private String cntrUploadFolder;

    @Value("${file.cntrAccessPath}")
    private String cntrAccessPath;

    /**
     * 导入清单
     */
    @RequestMapping("/cntrUpload/{orderNo}")
    public ResultUtil<List<YwOrderCntrVo>> cntrUpload(@PathVariable("orderNo")String orderNo , MultipartFile file, HttpServletRequest request) {
        UserDto user = TokenUtil.getUser(request);
        return ywOrderCntrService.cntrUpload(orderNo,file);
    }

    /**
     * 导入清单
     */
    @RequestMapping("/cntrUpload/{orderNo}/{type}")
    public ResultUtil<List<YwOrderCntrVo>> cntrUploadByType(@PathVariable("orderNo")String orderNo ,@PathVariable("type")String type , MultipartFile file, HttpServletRequest request) {
        UserDto user = TokenUtil.getUser(request);
        return ywOrderCntrService.cntrUpload(orderNo,type,file);
    }


    /**
     * 导入到达清单
     */
    @RequestMapping("/cntrDdUpload/{orderNo}")
    public ResultUtil<List<YwOrderCntrVo>> cntrDdUpload(@PathVariable("orderNo")String orderNo , MultipartFile file, HttpServletRequest request) {
        UserDto user = TokenUtil.getUser(request);
        return ywOrderCntrService.cntrDdUpload(orderNo,file);
    }

    /**
     * 保存上传的箱子
     */
    @PostMapping("/importCntrList")
    public ResultUtil<T> importCntrList(HttpServletRequest request, @RequestBody YwOrderVo vo) {
        return ywOrderCntrService.importCntrList(request,vo.getId() , vo.getCntrList());
    }

    /**
     * 查询箱明细
     */
    @RequestMapping("/getCntrList")
    public ResultUtil<List<YwOrderCntrVo>> getCntrList(@RequestBody YwOrderCntrVo vo) {
        Page<YwOrderCntrVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<YwOrderCntrVo> vos = ywOrderCntrService.getCntr(page, vo);
        return ResultUtil.build(vos,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 业务详情-箱明细-不分页
     */
    @RequestMapping("/getCntrListNoPage")
    public ResultUtil<List<YwOrderCntrVo>> getCntrListNoPage(@RequestBody YwOrderCntrVo vo) {
        List<YwOrderCntrVo> vos = ywOrderCntrService.getCntrListNoPage(vo);
        return ResultUtil.build(vos, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 上传集装箱图片压缩包文件
     * @param uploadFile
     * @return
     */
    @PostMapping("/uploadCntrImgZipFile")
    public ResultUtil<Map<String, List<CntrImgFile>>> uploadCntrImgZipFile(HttpServletRequest request, @RequestParam("uploadFile") MultipartFile uploadFile, YwOrderVo vo) {
        UserDto user = TokenUtil.getUser(request);
        vo.setUserId(user.getId());
        vo.setUserName(user.getRealname());
        return ywOrderCntrService.uploadCntrImgZipFile(uploadFile,vo, cntrUploadTempFolder,cntrUploadFolder,cntrAccessPath);
    }


    private final static String EXPORT_EXCEL_PATH = "static/download/客户委托清单模板.xlsx";
    private final static String EXPORT_HKEXCEL_PATH = "static/download/还空委托清单模板.xlsx";
    private final static String EXPORT_KJEXCEL_PATH = "static/download/空进委托清单模板.xlsx";

    private final static String EXPORT_EXCEL_DD_PATH = "static/download/班列到达导入模板.xlsx";

    /**
     * 清单导出模板下载
     */
    @GetMapping("/exportFile")
    public void export(HttpServletRequest request, HttpServletResponse response) {
        ywOrderCntrService.exportFile(request, response, EXPORT_EXCEL_PATH);
    }

    /**
     * 清单模板下载
     */
    @GetMapping("/exportFile/{type}")
    public void exportFile(HttpServletRequest request, HttpServletResponse response,@PathVariable("type")String type) {
        if("HK".equals(type)){
            ywOrderCntrService.exportFile(request, response, EXPORT_HKEXCEL_PATH);
        }else if("KJ".equals(type)){
            ywOrderCntrService.exportFile(request, response, EXPORT_KJEXCEL_PATH);
        }else{
            ywOrderCntrService.exportFile(request, response, EXPORT_EXCEL_PATH);
        }
    }

    /**
     * 清单导出模板下载
     */
    @GetMapping("/exportDdFile")
    public void exportDdFile(HttpServletRequest request, HttpServletResponse response) {
        ywOrderCntrService.exportFile1(request, response, EXPORT_EXCEL_DD_PATH);
    }

    /**
     * 新增箱信息
     */
    @PostMapping("/addCntrRet")
    public ResultUtil<T> addCntrRet(@RequestBody YwOrderCntr cntr, HttpServletRequest request) {
        UserDto user = TokenUtil.getUser(request);
        cntr.setUserId(user.getId());
        cntr.setCreateTime(LocalDateTime.now());
        return ywOrderCntrService.addCntrRet(cntr);
    }

    /**
     * 编辑箱信息
     */
    @PostMapping("/updateCntrById")
    public ResultUtil<T> updateCntrById(@RequestBody YwOrderCntr cntr, HttpServletRequest request) {
        UserDto user = TokenUtil.getUser(request);
        cntr.setUpdateUserId(user.getId());
        cntr.setUpdateTime(LocalDateTime.now());
        return ywOrderCntrService.updateOldCntrById(cntr);
    }

    /**
     * 删除箱信息
     */
    @PostMapping("/delBatchCntrRet")
    public ResultUtil<T> delBatchCntrRet(@RequestBody YwOrderVo vo, HttpServletRequest request) {
        UserDto user = TokenUtil.getUser(request);
        vo.setUpdateUserId(user.getId());
        vo.setUpdateUserName(user.getRealname());
        vo.setUpdateTime(LocalDateTime.now());
        return ywOrderCntrService.delBatchCntrRet(vo);
    }

    /**
     * 设置箱流向
     */
    @PostMapping("/updateCntrFlow")
    public ResultUtil<T> updateCntrFlow(@RequestBody YwOrderVo vo, HttpServletRequest request) {
        UserDto user = TokenUtil.getUser(request);
        vo.setUpdateUserId(user.getId());
        vo.setUpdateUserName(user.getRealname());
        vo.setUpdateTime(LocalDateTime.now());
        return ywOrderCntrService.updateCntrFlow(vo);
    }

    /**
     * 查询
     */
    @RequestMapping("/getPowerCntrList")
    public ResultUtil<List<YwOrderCntrVo>> getPowerCntrList(@RequestBody YwOrderCntrVo vo) {
        Page<YwOrderCntrVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<YwOrderCntrVo> vos = ywOrderCntrService.getPowerCntrList(page, vo);
        return ResultUtil.build(vos,page.getTotal(),page.getPages(),ResultUtilCodeEnum.SUCCESS);
    }
}
